using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Web;

namespace Developex.DatabaseWrapper
{
	/// <summary>
	/// Default database event and errors handler
	/// </summary>
	public class DefaultDatabaseEventHandler : DefaultDatabaseErrorHandler, IDatabaseEventHandler
	{
		private DateTime? currentDateTime = null;

		/// <summary>
		/// Get current datetime
		/// </summary>
		/// <returns></returns>
		public virtual DateTime GetCurrentDateTime()
		{
			if (currentDateTime == null)
				currentDateTime = DateTime.Now;
			return currentDateTime.Value;
		}

		/// <summary>
		/// Called before each query
		/// </summary>
		public virtual void OnResetBetweenQueries()
		{
			currentDateTime = null;
		}

		/// <summary>
		/// Get current user (for UpdatedBy, CreatedBy)
		/// </summary>
		/// <returns></returns>
		public virtual object GetCurrentUser()
		{
			if (HttpContext.Current != null && HttpContext.Current.User != null &&
				 HttpContext.Current.User.Identity != null && HttpContext.Current.User.Identity.IsAuthenticated)
			{
				return HttpContext.Current.User.Identity.Name;
			}
			return null;
		}
	}
}
